<template>
	<view>
		<!-- 1， -->
		<view class="select-view">
			<user-search @searchUser="searchUser" :selectUser="selectUser"></user-search>

		</view>
		<view class="select-view-zw"></view>

		<!-- 3，主要成员框 -->
		<user-data :groupUsers="groupUserData" @checkedRadio="checkedRadio"></user-data>

		<view class="zw"></view>
	</view>
</template>

<script>
	//1，导入子组件
	import userSearch from './user-search.vue'
	import userData from './user-data.vue'

	//2，引入网络请求
	import {
		getRelationListBySearch
	} from '@/api/http/msg/relation.js'

	export default {
		components: {
			userSearch,
			userData
		},
		data() {
			return {
				selectUser: [],
				groupUserData: [],
				userId: uni.getStorageSync('userId')
			}
		},
		methods: {
			getRelationListBySearch(relationType, searchName) {
				var that = this
				getRelationListBySearch(this.userId, relationType, searchName).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					var busInfo = res.data.busInfo
					for (var i = 0; i < busInfo.length; i++) {
						busInfo[i].isRadio = false
					}
					this.groupUserData = busInfo
				})
			},
			searchUser(e) {
				this.queryGroupUsers(this.groupId, e)
			},
			checkedRadio(e) {
				//console.log(e)
				if (e.isRadio) {
					for (var i = 0; i < this.groupUserData.length; i++) {
						if (this.groupUserData[i].userId == e.userId) {
							this.groupUserData[i].isRadio = !this.groupUserData[i].isRadio
							//console.log(123)
							for (var j = 0; j < this.selectUser.length; j++) {
								if (this.selectUser[j].userId == e.userId) {
									this.selectUser.splice(j, 1)

									//this.$refs.userHeadRef.changeUserNum(this.selectUser.length)
									//this.userNum = this.selectUser.length
								}
							}
						}
					}
				} else {
					for (var i = 0; i < this.groupUserData.length; i++) {
						if (this.groupUserData[i].userId == e.userId) {
							//console.log(this.groupUserData[i].isRadio)
							this.groupUserData[i].isRadio = !this.groupUserData[i].isRadio

							this.selectUser.push(e)

							//this.$refs.userHeadRef.changeUserNum(this.selectUser.length)
						}
					}
				}

				this.$emit('changeUsers', this.selectUser)
			},
		},
		mounted() {
			this.getRelationListBySearch(2, '')
		}
	}
</script>

<style lang="scss">
	.zw {
		width: 100%;
		height: 100rpx;
	}

	.select-view {
		width: 100%;
		height: 100rpx;
		background-color: #FFF;
		position: fixed;
		z-index: 9;
	}

	.select-view-zw {
		width: 100%;
		height: 100rpx;
	}
</style>